{% extends "base_generic.html" %}

{% block content %}
<div class="container">
  <!-- 书名与简介 -->
  <div class="card">
    <h2>{{ book.title }}</h2>
    <p><b>简介：</b>{{ book.summary }}</p>
    {% if user.is_authenticated %}
      <form method="post" action="{% url 'borrow-book' book.pk %}" style="display:inline;">
        {% csrf_token %}
        <button type="submit" class="btn">借阅</button>
      </form>
    {% else %}
      <span>（请登录后借阅）</span>
    {% endif %}
  </div>

  <!-- 评分与点赞 -->
  <div class="card" style="display: flex; align-items: center; justify-content: space-between;">
    <div>
      <b>平均评分：</b>
      <span style="font-size: 1.3em; color: #ff9800;">{{ book.avg_score|floatformat:1 }}</span> / 5
      <span style="color: #888;">（{{ book.ratings.count }}人评分）</span>
    </div>
    <div>
      <form method="post" action="{% url 'like-book' book.pk %}" style="display:inline;">
        {% csrf_token %}
        <button type="submit" class="btn" {% if user in book.likes.all.values_list 'user', flat=True %}disabled{% endif %}>
          👍 点赞（{{ book.likes.count }}）
        </button>
      </form>
    </div>
  </div>

  <!-- 评分表单 -->
  <div class="card">
    {% if user.is_authenticated %}
      <form method="post" action="{% url 'rate-book' book.pk %}" style="display: flex; align-items: center;">
        {% csrf_token %}
        <label style="margin-right: 10px;">我要评分：</label>
        <select name="score" required style="margin-right: 10px;">
          <option value="">请选择</option>
          {% for i in "12345" %}
            <option value="{{ i }}">{{ i }} 星</option>
          {% endfor %}
        </select>
        <button type="submit" class="btn">提交</button>
      </form>
    {% else %}
      <span>（请登录后评分）</span>
    {% endif %}
  </div>

  <!-- 书评区 -->
  <div class="card">
    <h3>书评</h3>
    {% for review in book.reviews.all %}
      <div style="border-bottom:1px solid #eee; padding:8px 0;">
        <b>{{ review.user.username }}</b>：{{ review.content }}
        <span style="color:#888; font-size:13px;">({{ review.created_at|date:'Y-m-d H:i' }})</span>
      </div>
    {% empty %}
      <div>暂无书评。</div>
    {% endfor %}
    {% if user.is_authenticated %}
      <form method="post" action="{% url 'review-book' book.pk %}" style="margin-top:12px;">
        {% csrf_token %}
        <textarea name="content" required style="width:100%;height:60px;resize:vertical;"></textarea>
        <button type="submit" class="btn" style="margin-top:6px;">发表书评</button>
      </form>
    {% else %}
      <span>（请登录后发表评论）</span>
    {% endif %}
  </div>
</div>
{% endblock %}
